<?php
$version="SVN";
/**
 * Simple Image Gallery
 * Version SVN
 *
 * http://code.google.com/p/sigallery/
 * 
 * This file displays thumbnails (and creates missing ones).
 *
 * License: http://www.opensource.org/licenses/bsd-license.php
 */

@require "config.php";
if(!$config_done) {
	// Default settings
	// DO NOT CHANGE THESE - MAKE CHANGES IN config.php INSTEAD

	$title="Simple Image Gallery";	
	
	$hidden_filenames=array(
	"index.php",
	"config.php"
	);

	$thumb_filename_prefix=".tn-";
}

function formatBytes($bytes, $precision = 2) { 
    $units = array('B', 'KB', 'MB', 'GB', 'TB'); 
   
    $bytes = max($bytes, 0); 
    $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); 
    $pow = min($pow, count($units) - 1); 
   
    $bytes /= pow(1024, $pow); 
   
    return round($bytes, $precision) . ' ' . $units[$pow]; 
} 

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
	<head>
		<title><?=$title?></title>
		<meta http-equiv="content-type" content="text/html; charset=utf-8" />
		<link rel="stylesheet" href="style.css" />
	</head>
	<body>
		<h1><?=$title?></h1>
		<?php
		flush();

		$d = dir(dirname($_SERVER['SCRIPT_FILENAME']));
		
		$entries=array();
		$i=0;
		while (false !== ($entry = $d->read())) {
			if(substr($entry,0,1) != "." && $entry != "index.php") {
				$entries[$i++]=$entry;
			}
		}
		$d->close();
		usort($entries,"strcasecmp");
		$i=0;
		if(function_exists("finfo_open")) {
			$finfo=finfo_open();
		}

		if(isset($_GET['offset']) && is_numeric($_GET['offset'])) {
			$offset=$_GET['offset'];
		}
		while($entry=$entries[$i++]) {
			if($i<$offset) {
				continue;
			}
			if($i-$offset > 100) {
				break;
			}
			if(substr($entry,0,1) != "." && !in_array($entry, $hidden_filenames)) {
				if(is_file($entry)) { 
					$filesize=formatBytes(filesize($entry));
					if(isset($finfo)) {
						$info=finfo_file($finfo, $entry);
					} else {
						$info="Unknown file";
						if(function_exists("getimagesize")) {
							$is=getimagesize($entry);
							if($is!==false) {
								$info=image_type_to_mime_type($is[2]);
							}
						}
					}
					if(stristr($info, "image")) {
						$thumb=$thumb_filename_prefix.$entry;
						if(!file_exists($thumb)) {
							if(is_writable(".")) {
								$thumb="create_thumb.php?file=".$entry;
							} else {
								$thumb="";
								$error="I need to be able to write to this directory in order to create thumbnails for you.";
							}
						}
						?>
						<div class="file">
							<a class="thumb" href="<?php echo $entry; ?>"><img src="<?php echo $thumb; ?>" /></a><br />
							<a class="filename" href="<?php echo $entry; ?>">
								<?php echo $entry; ?>
							</a>
							<p class="filetype"><?=$info?></p>
							<p class="filesize"><?=$filesize?></p>
							<?php
							if(isset($error)) {
								echo $error;
							}
							?>
						</div>
						<?php
					} else {
						?>
						<div class="file">
							<a class="thumb" href="<?php echo $entry; ?>"><img src="unknown.png" /></a><br />
							<a class="filename" href="<?php echo $entry; ?>">
								<?php echo $entry; ?>
							</a>
							<p class="filetype"><?=$info?></p>
							<p class="filesize"><?=$filesize?></p>
						</div>
						<?php
					}

				} elseif(is_dir($entry)) {
					?>
					<div class="file">
						<a class="thumb" href="<?php echo $entry; ?>"><img src="folder.png" /></a><br />
						<a class="filename" href="<?php echo $entry; ?>">
							<?php echo $entry; ?>
						</a>
						<p class="filetype">Directory</p>
					</div>
					<?php
				} else {
					echo $entry.": error, not a file or directory<br />";
				}
			}
		}

		?>
		<div class="end"></div>
		<hr />
		<div class="footer">
			<div class="left"><a href="?offset=<?php echo $offset-100;?>">&lt;&lt; Previous 100</a></div>
			<div class="center">Showing files <?php echo ($offset)?$offset:'1';?>-<?php echo $offset+100;?> of <?php echo count($entries)?></div>
			<div class="right"><a href="?offset=<?php echo $offset+100;?>">Next 100 &gt;&gt;</a></div>
			<div class="end"></div>
		</div>
		<hr />
		<p class="footer">
			This image gallery is using <a href="http://code.google.com/p/sigallery/">Simple Image Gallery</a> version <?=$version?>
		</p>
	</body>
</html>
